package com.health.medicine.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.api.model.producer.ProducerRegisterRequest;
import com.health.common.common.ErrorCode;
import com.health.common.exception.BusinessException;
import com.health.common.model.vo.UserKeyVO;
import com.health.common.service.util.SDKUtils;
import com.health.medicine.mapper.ManufacturerMapper;
import com.health.medicine.model.dto.producer.ProducerUpdateRequest;
import com.health.medicine.model.entity.Manufacturer;
import com.health.medicine.service.ManufacturerService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.fisco.bcos.sdk.v3.codec.ContractCodecException;
import org.fisco.bcos.sdk.v3.model.TransactionReceipt;
import org.fisco.bcos.sdk.v3.transaction.manager.AssembleTransactionProcessor;
import org.fisco.bcos.sdk.v3.transaction.model.exception.TransactionBaseException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.health.common.constant.ContractConstants.ADD_MANUFACTURE;

/**
* @author zhexueqi
* @description 针对表【manufacturer(药品生产厂家信息表)】的数据库操作Service实现
* @createDate 2024-08-30 13:09:28
*/
@Service
@Slf4j
public class ManufacturerServiceImpl extends ServiceImpl<ManufacturerMapper, Manufacturer>
    implements ManufacturerService {

    @Resource
    private SDKUtils sdkUtils;

    @Value("${system.contract.contractName}")
    private String CONTRACT_NAME;

    @Value("${system.contract.address}")
    private String CONTRACT_ADDRESS;

    @Override
    public Boolean createManufacturerToMysql(ProducerRegisterRequest producerRegisterRequest) {
        Manufacturer manufacturer = new Manufacturer();
        BeanUtil.copyProperties(producerRegisterRequest,manufacturer);
        manufacturer.setAccount(producerRegisterRequest.getManufacturerAccount());
        boolean save = this.save(manufacturer);
        if (!save){
            throw new BusinessException(ErrorCode.SQL_ERROR);
        }
        return true;
    }

    @Override
    public boolean createManufacturer(UserKeyVO userKeyVO, String userAccount) throws TransactionBaseException, ContractCodecException {
        QueryWrapper<Manufacturer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",userAccount);
        Manufacturer manufacturer = this.getOne(queryWrapper);
        if (manufacturer == null){
            return false;
        }
        List<Object> funcParams = new ArrayList<>();
        funcParams.add(manufacturer.getContactLocation());
        funcParams.add(manufacturer.getName());
        funcParams.add(manufacturer.getLicenseNumber());

        //调用合约方法
        Object o = sdkUtils.sendTransaction(CONTRACT_NAME, CONTRACT_ADDRESS, ADD_MANUFACTURE, funcParams, userKeyVO.getPrivateKey());
        if (o == null){
            return false;
        }
        return true;
    }

    @Override
    public Boolean updateInfo(ProducerUpdateRequest producerUpdateRequest) {
        if (producerUpdateRequest== null){
            throw new BusinessException(ErrorCode.PARAMS_NULL,"参数为空");
        }
        Manufacturer manufacturer = new Manufacturer();
        BeanUtil.copyProperties(producerUpdateRequest,manufacturer);
        boolean update = this.update(manufacturer,new QueryWrapper<Manufacturer>().eq("id",producerUpdateRequest.getId()));
        if (!update) {
            throw new BusinessException(ErrorCode.SQL_ERROR);
        }
        return true;
    }
}




